function BookmarksDbHelper() {
	var api = {};

	var dbVersion = 1;

	api.checkVersion = function() {
		var db = Ti.Database.install('/database/bookmarks.db', 'bookmarks');
		var rs = db.execute('SELECT version from version limit 1');
		while (rs.isValidRow()) {
			if (rs.fieldByName('version') < dbVersion) {
				rs.close();
				db.close();
			} else {
				rs.close();
				db.close();
			}
			break;
		}
	}

	api.getRows = function(lang) {
		var db = Ti.Database.install('/database/bookmarks.db', 'bookmarks');

		var tableData = [];
		var index = [];

		var prevHeader = "";
		var header = "";
		var count = 0;

		var sort = lang == "mal" ? "title_ml" : "title_en";
		var title;

		rs = db.execute('SELECT song_id, title_en, title_ml, filename_ml, filename_en FROM songs order by ' + sort);
		while (rs.isValidRow()) {
			title = lang == "mal" ? rs.fieldByName('title_ml') : rs.fieldByName('title_en');
			header = title.substr(0, 1);

			if (prevHeader != header) {
				index.push({
					title : header,
					index : count
				});
				tableData.push({
					ml : rs.fieldByName('filename_ml'),
					en : rs.fieldByName('filename_en'),
					title : title,
					title_ml : rs.fieldByName('title_ml'),
					filter : rs.fieldByName('title_en'),
					header : header
				});
				prevHeader = header;
			} else {
				tableData.push({
					ml : rs.fieldByName('filename_ml'),
					en : rs.fieldByName('filename_en'),
					title : title,
					title_ml : rs.fieldByName('title_ml'),
					filter : rs.fieldByName('title_en')
				});
			}
			count++;
			rs.next();
		}

		db.close();

		return {
			data : tableData,
			index : index
		};
	};

	api.insertRow = function(data) {
		var db = Ti.Database.install('/database/bookmarks.db', 'bookmarks');
		var sql = "select * from SONGS where filename_ml = '" + data.ml + "'";
		var rs = db.execute(sql);
		if (rs.isValidRow()) {
			rs.close();
			db.close();
			this.showNotification('Song exists in bookmarks', 1000);
		} else {
			sql = "INSERT INTO SONGS(title_ml, title_en, filename_ml, filename_en) values('" + data.title_ml + "', '" + data.title_en + "', '" + data.ml + "', '" + data.en + "')";
			db.execute(sql);
			db.close();
			this.showNotification('Song bookmarked', 1000);
		}
	};

	api.deleteRow = function(data) {
		var db = Ti.Database.install('/database/bookmarks.db', 'bookmarks');
		var sql = "DELETE FROM SONGS where filename_ml = '" + data.ml + "'";
		db.execute(sql);
		db.close();
		this.showNotification('Song deleted from bookmarks', 1000);
	};
	
	api.deleteAll = function() {
		var db = Ti.Database.install('/database/bookmarks.db', 'bookmarks');
		var sql = "DELETE FROM SONGS";
		db.execute(sql);
		db.close();
		this.showNotification('Songs deleted from bookmarks', 1000);
	};

	api.showNotification = function(customMessage, interval) {
		if (Ti.Platform.osname=='iphone' || Ti.Platform.osname=='ipad') {
			// window container
			var indWin = Titanium.UI.createWindow();

			//  view
			var indView = Titanium.UI.createView({
				height : 100,
				width : 250,
				borderRadius : 10,
				backgroundColor : '#000',
				opacity: 0.9
			});

			indWin.add(indView);

			// message
			var message = Titanium.UI.createLabel({
				text : customMessage && typeof (customMessage !== 'undefined') ? customMessage : L('please_wait'),
				color : '#fff',
				width : 'auto',
				height : 'auto',
				textAlign : 'center'
			});

			indView.add(message);
			indWin.open();

			interval = interval ? interval : 2500;
			setTimeout(function() {
				indWin.close({
					opacity : 0,
					duration : 1000
				});
			}, interval);
		}
	};

	return api;
};

module.exports = BookmarksDbHelper;
